package com.wy.tingshu.servlet.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wy.tingshu.service.UserService;
import com.wy.tingshu.util.Log;
import com.wy.tingshu.view_object.ResultAlbumView;
import com.wy.tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 美女
 * @date 2022/08/03 02:48
 **/
@WebServlet("/studio/user/currentUser.json")
public class CheckJsonServlet extends HttpServlet {
    private final ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.用户登录则获取用户信息
        UserVO userVO=null;
        HttpSession session=req.getSession(false);
        if(session!=null){
            userVO=(UserVO) session.getAttribute("currentUser");//session的用户是登陆时放入的
        }

        String json=objectMapper.writeValueAsString(userVO);
        Log.println("序列化后的结果是："+json);
        //响应该字符串
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer=resp.getWriter();
        Log.println("将json写入响应体中");
        writer.println(json);
    }
}
